package com.service;

import android.app.Activity;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.notifation.DefaultNotifyBuilder;
import com.notifation.NotifyManager;
import com.util.InfoEventMessage;
import com.util.MyLog;
import com.xiaochun.hxhj.MainActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.Calendar;

public class NotifationService extends Service {

    private Handler handlerdelay;
    private SharedPreferences sharedPreferences;

    @Override
    public void onCreate() {
        MyLog.i("通知栏","onCreate - Thread ID = " + Thread.currentThread().getId());
        handlerdelay = new Handler();
        sharedPreferences = getSharedPreferences("userinfo", Activity.MODE_PRIVATE);
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        MyLog.i("通知栏", "onStartCommand - startId = " + startId + ", Thread ID = " + Thread.currentThread().getId());

        Calendar cal= Calendar.getInstance();
        int y=cal.get(Calendar.YEAR);
        int m=cal.get(Calendar.MONTH);
        int d=cal.get(Calendar.DATE);
        int h=cal.get(Calendar.HOUR_OF_DAY);
        int mi=cal.get(Calendar.MINUTE);
        int s=cal.get(Calendar.SECOND);
//        System.out.println(h+":"+mi+"现在时刻是"+y+"年"+m+"月"+d+"日"+h+"时"+mi+"分"+s+"秒");
        handlerdelay.postDelayed(new Runnable() {
            @Override
            public void run() {
                Calendar cal= Calendar.getInstance();
                int h=cal.get(Calendar.HOUR_OF_DAY);
                int mi=cal.get(Calendar.MINUTE);
                String tixingtime = sharedPreferences.getString("tixingtime", "08:00");
//                System.out.println(h+":"+mi+"   现在时刻是   "+tixingtime);
                String mtime = mi+"";
                if (mtime.length() <2){
                    mtime = "0"+mi;
                }
                String time = h+":"+mtime;
                if (tixingtime.equals(time)){
                    EventBus.getDefault().post(new InfoEventMessage("时间通知栏"));
                    handlerdelay.postDelayed(this, 60000);
                }else{
                    handlerdelay.postDelayed(this, 1000);
                }

            }
        },0);


        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        MyLog.i("通知栏", "onBind - Thread ID = " + Thread.currentThread().getId());
        return null;
    }

    @Override
    public void onDestroy() {
        MyLog.i("通知栏", "onDestroy - Thread ID = " + Thread.currentThread().getId());
        super.onDestroy();
    }

    private void notifySysMessage() {
        String key = "SysMessage#系统消息";
        int requestCode = NotifyManager.getInstance(getApplicationContext()).initNotifyId(key);
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        intent.putExtra("msgContent", "系统消息" + ":\n\n" + "训练时间到，赶快来提升自己的记忆力吧！");
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        DefaultNotifyBuilder defaultNotifyBuilder = new DefaultNotifyBuilder("系统消息", "训练时间到，赶快来提升自己的记忆力吧！")
                .setChannelId("goschooltime")
                .setContentIntent(pendingIntent);
        NotifyManager.getInstance(getApplicationContext()).showDefaultNotification(key, defaultNotifyBuilder);
    }



}